package com.ray.monitor.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ray.api.service.UcenterApiService;
import com.ray.auth.system.table.entity.Button;
import com.ray.monitor.service.HostService;
import com.ray.monitor.service.InvokeRedisManager;
import com.ray.monitor.service.ServicesService;
import com.ray.monitor.table.vo.MethodRankBO;
import com.ray.monitor.table.vo.ServiceBO;
import com.ray.monitor.util.DataGridUtil;
import com.xiaoleilu.hutool.util.ObjectUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.web.controller.WebController;

/**
 * Created by zxg on 15/11/19.
 */
@Controller
@RequestMapping("/monitor/dubbo/service")
public class ServicesController extends WebController{
	@Autowired
	UcenterApiService ucenterApiService;
    @Autowired
    private ServicesService servicesService;
 
    @RequestMapping(value = "view")
	@RequiresPermissions("monitor:dubbo:service:view")
	public ModelAndView view(String menuId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView) {
		LoginUser user = getLoginUser();
		// 查询页面拥有的资源
		Result<Map<String, List<Button>>> buttonsResult = ucenterApiService.queryUserButtonsWithMenu(user, menuId);
		if (buttonsResult.codeSuccess()) {
			modelAndView.addObject("buttons", buttonsResult.getData());
		}
		modelAndView.setViewName("monitor/dubbo/service/view");
		return modelAndView;
	}

	
	/**
	 * 查询页面
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "query")
	@RequiresPermissions("monitor:dubbo:service:query")
	@ResponseBody
	public ResponseEntity<Map<String, Object>> query(String serviceName,HttpServletRequest request,HttpServletResponse respose) {
		  PageDto<ServiceBO> page = this.createJQPage(request);
          Map<String, ServiceBO> allServicesMap = servicesService.getServiceBOMap();
          List<ServiceBO> appList = new ArrayList<>();
          for (Map.Entry<String, ServiceBO> serviceBOEntry : allServicesMap.entrySet()) {
              ServiceBO serviceBO = serviceBOEntry.getValue();
              if(StrUtil.isBlank(serviceName) || StrUtil.equals(serviceName, serviceBO.getServiceName())){
            	  appList.add(serviceBO);
              }
              
          }
         page.setRecords(appList);
         page.setTotal(appList.size());
		return doSuccess(DataGridUtil.dataConvert(page));

	}
	
	/**
	 * 查询页面
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "method/query")
	@RequiresPermissions("monitor:dubbo:service:query")
	@ResponseBody
	public ResponseEntity<Map<String, Object>> methodQuery(String serviceName,HttpServletRequest request,HttpServletResponse respose) {
		  PageDto<MethodRankBO> page = this.createJQPage(request);
          Map<String, ServiceBO> allServicesMap = servicesService.getServiceBOMap();
          List<MethodRankBO> list = new ArrayList<>();
          ServiceBO serviceBO = allServicesMap.get(serviceName);
          if(ObjectUtil.isNotNull(serviceBO)){
        	Set<String> sets =  serviceBO.getMethods();
        	for(String names:sets){
        		for(String name : names.split(",")){
            		list.add(servicesService.getOneDay(serviceName, name));
        		}
        	}
          }
         page.setRecords(list);
         page.setTotal(list.size());
		return doSuccess(DataGridUtil.dataConvert(page));

	}

}
